package com.iiiss.template.ssh.impl.daos;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.iiiss.template.ssh.core.beans.ISnsUser;
import com.iiiss.template.ssh.core.daos.ISnsUserDao;

/*
 * TemplateSSH - com.iiiss.template.ssh.impl.daos - SnsUserDao.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * SnsUserDao.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-8-9] Mingye Cheng - Initial version established.
 * [2011-8-27] Mingye Cheng - Using generic types for Lists.
 */
@Transactional
public class SnsUserDao extends UserDao implements ISnsUserDao {
	public boolean isFriend(ISnsUser currentUser, ISnsUser targetUser) {
		return currentUser.getFriends().contains(targetUser)
				&& targetUser.getFollowers().contains(currentUser);
	}

	public void addFriend(ISnsUser currentUser, ISnsUser targetUser) {
		currentUser.getFriends().add(targetUser);
		targetUser.getFollowers().add(currentUser);
	}

	public int countFriends(ISnsUser targetUser) {
		return targetUser.getFriends().size();
	}

	public int countFollowers(ISnsUser targetUser) {
		return targetUser.getFollowers().size();
	}

	@SuppressWarnings("unchecked")
	public <T extends ISnsUser> List<T> getFriends(T targetUser) {
		return (List<T>) targetUser.getFriends();
	}

	@SuppressWarnings("unchecked")
	public <T extends ISnsUser> List<T> getFollowers(T targetUser) {
		return (List<T>) targetUser.getFollowers();
	}

	public void removeFriend(ISnsUser currentUser, ISnsUser targetUser) {
		currentUser.getFriends().remove(targetUser);
		targetUser.getFollowers().remove(currentUser);
	}
}
